<?php

class ReservationForm extends CFormModel
{
	public $itemId;
	public $quantity;

	/**
	 * Declares the validation rules.
	 */
	public function rules()
	{
		return array(
			array('itemId, quantity','required',
					'message'=>Yii::t('app','Please enter a value for {attribute}.')),
			array('quantity','numerical','integerOnly'=>true,
					'message'=>Yii::t('app','{attribute} must be an integer.')),
			array('quantity','validateQuantity'),
		);
	}

	/**
	 * Declares customized attribute labels.
	 * If not declared here, an attribute would have a label that is
	 * the same as its name with the first letter in upper case.
	 */
	public function attributeLabels()
	{
		return array(
			'quantity' => Yii::t('label', 'Quantity'),
		);
	}

	public function validateQuantity()
	{
		$item=Item::model()->findByPk($this->itemId);
		if($this->quantity>$item->getRemainingQuantity())
			$this->addError('quantity',Yii::t('app','Quantity is too large.'));
		else if($this->quantity<1)
			$this->addError('quantity',Yii::t('app','Quantity is too small.'));
	}
}
